package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuncheng.spcyApi.entity.SpcyRsYwzdxx;
import com.yuncheng.spcyApi.mapper.SpcyRsYwzdxxMapper;
import com.yuncheng.spcyApi.service.ISpcyRsYwzdxxService;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 审评查验-业务字段选项
 * @Author: yuncheng
 * @Date: 2023-03-29
 * @Version: V1.0
 */
@Service
public class SpcyRsYwzdxxServiceImpl extends BaseServiceImpl<SpcyRsYwzdxxMapper, SpcyRsYwzdxx> implements ISpcyRsYwzdxxService {

    @Resource
    private SpcyRsYwzdxxMapper spcyRsYwzdxxMapper;

    @Override
    public String queryFzdnr(String fywflcode, String fzdlx1) {
        if (StringUtils.isBlank(fywflcode)){
            return "";
        }
        /*List<SpcyRsYwzdxx> list = list(new LambdaQueryWrapper<SpcyRsYwzdxx>()
                .eq(SpcyRsYwzdxx::getFywflcode, fywflcode)
                .eq(SpcyRsYwzdxx::getFzdlx1, fzdlx1));
        if (CollectionUtil.isNotEmpty(list)){
            return list.get(0).getFzdnr();
        }*/
        List<String> list = spcyRsYwzdxxMapper.selectFzdnrByFywflcodeAndFzdlx1(fywflcode, fzdlx1);
        if (CollectionUtil.isNotEmpty(list)){
            return list.get(0);
        }
        return "";
    }

    @Override
    public String querySqsxZdywId(String fywflcode) {
        /*if (StringUtils.isBlank(fywflcode)){
            fywflcode = "001002";
        }*/
        return this.queryFzdnr(fywflcode, "法律法规指导文件");
    }

    @Override
    public String querySqsxDefaultJczj(String fywflcode) {
        return this.queryFzdnr(fywflcode, "默认现场检查章节目录");
    }

    @Override
    public boolean querySqsxXcjcJcqkzsCompleteState(String sqsxid, boolean updateWcState, boolean updateYwcState) {
        String  state = this.queryFzdnr(sqsxid, "现场检查情况专述完成状态");
        if (StringUtils.isBlank(state)) {
            state = "未完成";

            SpcyRsYwzdxx ywzdxx = new SpcyRsYwzdxx();
            ywzdxx.setId(GetUuIdUtils.ReplaceUuId());
            ywzdxx.setFywflcode(sqsxid);
            ywzdxx.setFzdlx1("现场检查情况专述完成状态");
            ywzdxx.setFzdnr(state);
            save(ywzdxx);
        }

        if (updateWcState){

            LambdaUpdateWrapper<SpcyRsYwzdxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsYwzdxx::getFywflcode, sqsxid);
            updateWrapper.eq(SpcyRsYwzdxx::getFzdlx1, "现场检查情况专述完成状态");
            if (updateYwcState) {

                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("已完成");

                update(updateBean, updateWrapper);

            }else {
                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("未完成");

                update(updateBean, updateWrapper);
            }

            return true;
        }

        return state.equals("已完成");
    }

    @Override
    public boolean querySqsxXcjcFxwtQxCompleteState(String sqsxid, boolean updateWcState, boolean updateYwcState) {

        String  state = this.queryFzdnr(sqsxid, "现场检查发现问题缺陷完成状态");
        if (StringUtils.isBlank(state)) {
            state = "未完成";

            SpcyRsYwzdxx ywzdxx = new SpcyRsYwzdxx();
            ywzdxx.setId(GetUuIdUtils.ReplaceUuId());
            ywzdxx.setFywflcode(sqsxid);
            ywzdxx.setFzdlx1("现场检查发现问题缺陷完成状态");
            ywzdxx.setFzdnr(state);
            save(ywzdxx);
        }

        if (updateWcState){

            LambdaUpdateWrapper<SpcyRsYwzdxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsYwzdxx::getFywflcode, sqsxid);
            updateWrapper.eq(SpcyRsYwzdxx::getFzdlx1, "现场检查发现问题缺陷完成状态");
            if (updateYwcState) {

                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("已完成");

                update(updateBean, updateWrapper);

            }else {
                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("未完成");

                update(updateBean, updateWrapper);
            }

            return true;
        }

        return state.equals("已完成");
    }

    @Override
    public boolean querySqsxXcjcJcjlCompleteState(String sqsxid, boolean updateWcState, boolean updateYwcState) {

        String state = this.queryFzdnr(sqsxid, "现场检查结论完成状态");
        if (StringUtils.isBlank(state)) {
            state = "未完成";

            SpcyRsYwzdxx ywzdxx = new SpcyRsYwzdxx();
            ywzdxx.setId(GetUuIdUtils.ReplaceUuId());
            ywzdxx.setFywflcode(sqsxid);
            ywzdxx.setFzdlx1("现场检查结论完成状态");
            ywzdxx.setFzdnr(state);
            save(ywzdxx);
        }

        if (updateWcState){
            LambdaUpdateWrapper<SpcyRsYwzdxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsYwzdxx::getFywflcode, sqsxid);
            updateWrapper.eq(SpcyRsYwzdxx::getFzdlx1, "现场检查结论完成状态");

            if (updateYwcState) {

                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("已完成");

                update(updateBean, updateWrapper);

            }else {
                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("未完成");

                update(updateBean, updateWrapper);
            }
        }


        return state.equals("已完成");
    }

    @Override
    public boolean querySqsxZhpdScyjCompleteState(String sqsxid, boolean updateWcState, boolean updateYwcState) {
        String state = this.queryFzdnr(sqsxid, "综合评定审查意见完成状态");
        if (StringUtils.isBlank(state)) {
            state = "未完成";

            SpcyRsYwzdxx ywzdxx = new SpcyRsYwzdxx();
            ywzdxx.setId(GetUuIdUtils.ReplaceUuId());
            ywzdxx.setFywflcode(sqsxid);
            ywzdxx.setFzdlx1("综合评定审查意见完成状态");
            ywzdxx.setFzdnr(state);
            save(ywzdxx);
        }

        if (updateWcState){
            LambdaUpdateWrapper<SpcyRsYwzdxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsYwzdxx::getFywflcode, sqsxid);
            updateWrapper.eq(SpcyRsYwzdxx::getFzdlx1, "综合评定审查意见完成状态");

            if (updateYwcState) {

                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("已完成");

                update(updateBean, updateWrapper);

            }else {
                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("未完成");

                update(updateBean, updateWrapper);
            }
        }


        return state.equals("已完成");
    }

    @Override
    public boolean querySqsxZhpdBgsCompleteState(String sqsxid, boolean updateWcState, boolean updateYwcState) {
        String state = this.queryFzdnr(sqsxid, "综合评定报告书完成状态");
        if (StringUtils.isBlank(state)) {
            state = "未完成";

            SpcyRsYwzdxx ywzdxx = new SpcyRsYwzdxx();
            ywzdxx.setId(GetUuIdUtils.ReplaceUuId());
            ywzdxx.setFywflcode(sqsxid);
            ywzdxx.setFzdlx1("综合评定报告书完成状态");
            ywzdxx.setFzdnr(state);
            save(ywzdxx);
        }

        if (updateWcState){
            LambdaUpdateWrapper<SpcyRsYwzdxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsYwzdxx::getFywflcode, sqsxid);
            updateWrapper.eq(SpcyRsYwzdxx::getFzdlx1, "综合评定报告书完成状态");

            if (updateYwcState) {

                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("已完成");

                update(updateBean, updateWrapper);

            }else {
                SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
                updateBean.setFzdnr("未完成");

                update(updateBean, updateWrapper);
            }
        }


        return state.equals("已完成");
    }

    @Override
    public String queryOperationGuideInJcybxd(String userid, boolean updateWcState, boolean calcelWcState) {
        String state = this.queryFzdnr(userid, "操作指引-检查员报销单状态");
        if (StringUtils.isBlank(state)) {
            state = "0";

            SpcyRsYwzdxx ywzdxx = new SpcyRsYwzdxx();
            ywzdxx.setId(GetUuIdUtils.ReplaceUuId());
            ywzdxx.setFywflcode(userid);
            ywzdxx.setFzdlx1("操作指引-检查员报销单状态");
            ywzdxx.setFzdnr(state);
            save(ywzdxx);
        }

        if (updateWcState && state.equals("0")){
            state = "1";
            LambdaUpdateWrapper<SpcyRsYwzdxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsYwzdxx::getFywflcode, userid);
            updateWrapper.eq(SpcyRsYwzdxx::getFzdlx1, "操作指引-检查员报销单状态");

            SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
            updateBean.setFzdnr(state);

            update(updateBean, updateWrapper);
        }

        if (calcelWcState && state.equals("1")){
            state = "0";
            LambdaUpdateWrapper<SpcyRsYwzdxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsYwzdxx::getFywflcode, userid);
            updateWrapper.eq(SpcyRsYwzdxx::getFzdlx1, "操作指引-检查员报销单状态");

            SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
            updateBean.setFzdnr(state);

            update(updateBean, updateWrapper);
        }

        return state;
    }

    @Override
    public String selectOperationGuideInXcjcUser(String userid, boolean updateWcState, boolean calcelWcState) {
        String state = this.queryFzdnr(userid, "操作指引-用户现场检查状态");
        if (StringUtils.isBlank(state)) {
            state = "0";

            SpcyRsYwzdxx ywzdxx = new SpcyRsYwzdxx();
            ywzdxx.setId(GetUuIdUtils.ReplaceUuId());
            ywzdxx.setFywflcode(userid);
            ywzdxx.setFzdlx1("操作指引-用户现场检查状态");
            ywzdxx.setFzdnr(state);
            save(ywzdxx);
        }

        if (updateWcState && state.equals("0")){
            state = "1";

            LambdaUpdateWrapper<SpcyRsYwzdxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsYwzdxx::getFywflcode, userid);
            updateWrapper.eq(SpcyRsYwzdxx::getFzdlx1, "操作指引-用户现场检查状态");

            SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
            updateBean.setFzdnr(state);

            update(updateBean, updateWrapper);
        }

        if (calcelWcState && state.equals("1")){
            state = "0";
            LambdaUpdateWrapper<SpcyRsYwzdxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsYwzdxx::getFywflcode, userid);
            updateWrapper.eq(SpcyRsYwzdxx::getFzdlx1, "操作指引-用户现场检查状态");

            SpcyRsYwzdxx updateBean = new SpcyRsYwzdxx();
            updateBean.setFzdnr(state);

            update(updateBean, updateWrapper);
        }

        return state;
    }

    @Override
    public String selectWlyctsmContent(String code) {
        return this.queryFzdnr(code, "无利益冲突声明");
    }

    @Override
    public String selectJcycnsContent(String code) {
        return this.queryFzdnr(code, "现场检查检查员承诺书");
    }

    @Override
    public String selectXcjcjlContent(String code) {
        return this.queryFzdnr(code, "现场检查纪律");
    }

    @Override
    public String selectPxSjImportTemplate() {
        return this.queryFzdnr("3781", "试卷导入模板");
    }

    @Override
    public boolean verifySfyjOperation(String sqsxid, boolean isOperation) {
        if (isOperation){

            if (StringUtils.isBlank(this.queryFzdnr(sqsxid, "现场检查报告是否已操作签名按钮"))) {
                SpcyRsYwzdxx ywzdxx = new SpcyRsYwzdxx();
                ywzdxx.setId(GetUuIdUtils.NotReplaceUuId());
                ywzdxx.setFywflcode(sqsxid);
                ywzdxx.setFzdlx1("现场检查报告是否已操作签名按钮");
                ywzdxx.setFzdnr("1");

                save(ywzdxx);

                return false;
            }

            return true;
        }else {
            return StringUtils.isNotBlank(this.queryFzdnr(sqsxid, "现场检查报告是否已操作签名按钮"));
        }
    }
}
